
local HandbookHelper = require("app.handBook.HandbookHelper")
local SecondLevelListMenu = require("app.widget.listmenu.SecondLevelListMenu")
local fileName = string.getModeName(...)
local HandbookView = class(fileName, app.mvc.ViewBase)
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local HandbookRender = import(".renders.HandbookRender")
local HankbookCombatPowerPanel = import("..views.HankbookCombatPowerPanel")
HandbookView.VIEW_ROOT_RES = Res.HandBooKUi

local directoryChallengeConst = Constant.DirectoryChallengeConst

function HandbookView:init()
    self._ui["Panel/Button_back"]:addClickEventListener(function()
			executeFunc(self._onCloseView)
	end)
	self._hankbookCombatPowerPanel = HankbookCombatPowerPanel.create(self._ui["Panel/radar"])

	self._menuList = SecondLevelListMenu.new(self._ui["Panel/ScrollView1"], {
		focusChangeCallback = handler(self, self._onSwitchPanel),
	})


	self._contentListView = ListViewVertical.new(self._ui["Panel/ScrollView"], {
		renderClass = HandbookRender,
		perNum = 4,
		cellEventProxy = function(eventName, data)
			if not data then
				return
			end
			local config = data.config
			local resourceData = GD:getSpecialResource(config.feature)
			if resourceData then
				display.resourceGotoFeature({feature = resourceData[1], param = resourceData[3]})
			end
		end,
	})
end
--
function HandbookView:reloadMenu(subKey, redPointData)
	self._data = HandbookHelper.getHandbookList()
	local firstSelect = 1
	local secondSelect = nil
	if subKey then
		for k, v  in pairs(self._data) do
			if v.subKey == subKey then
				firstSelect = k
				break
			end
			local isFind = false
			if v.children then
				for k2, v2 in pairs(v.children) do
					if v2.subKey == subKey then
						firstSelect = k
						secondSelect = k2
						isFind = true
						break
					end
				end
				if isFind then
					break
				end
			end
		end
	end

	--判断我要变强是否有可领取的东西
	if table.nums(redPointData) > 0 then
		for k,v in pairs(self._data) do
			if redPointData[tostring(v.subKey)] then
				v.redPoint = true
			end
		end
	end


	self._menuList:reloadMenuData(self._data, {
		firstSelect = firstSelect,
		secondSelect = secondSelect,
	})
end

function HandbookView:_onSwitchPanel(data)
	if not data then
		return
	end

	self._selectedSubKey = data.subKey
	 if data.subKey == directoryChallengeConst then
	 	-- 我要变强2
	 	 self._ui["Panel/ScrollView"]:setVisible(false)
		 self._ui["Panel/Panel_bg/bg03"]:setVisible(false)
		 self._hankbookCombatPowerPanel:setVisible(false)
		 self._hankbookCombatPowerPanel:onOperate(self._onOperate)
		 --准备向服务器请求相关数据显示
		 executeFunc(self._onOperate,"getChallengeLVInfo")
	 else
		 self._ui["Panel/ScrollView"]:setVisible(true)
		 self._ui["Panel/Panel_bg/bg03"]:setVisible(true)
		 self._hankbookCombatPowerPanel:setVisible(false)
		local contentsData = data.contents or {}
		--
		for k, v in pairs(contentsData) do
			local config = v.config
			v.curNum = HandbookHelper.getItemProgress(checknumber(config.tipsSource))
		end

		local function sort( a, b )
			local configA = a.config
			local configB = b.config
			local sortA = tonumber( configA.sort )
			local sortB = tonumber( configB.sort )

			if configA.sortP and 0 ~= tonumber( configA.sortP ) then -- 存在动态排序权重
				local curNum = a.curNum
				if curNum and 0 >= curNum then
					sortA = tonumber( configA.sortP )
				end
			end
			if configB.sortP and 0 ~= tonumber( configB.sortP ) then -- 存在动态排序权重
				local curNum = b.curNum
				if curNum and 0 >= curNum then
					sortB = tonumber( configB.sortP )
				end
			end

			if sortA == sortB then
				return tonumber( configA.ID ) < tonumber( configB.ID )
			else
				return sortA < sortB
			end
		end

    	table.sort( contentsData, sort )
		self._contentListView:reloadData(contentsData, {startIndex = 1})
	 end
end

--刷新变强挑战
function HandbookView:refreshDirectoryChallengePanel(data)
	if self._selectedSubKey == directoryChallengeConst then
		self._hankbookCombatPowerPanel:setData(data)
		self._hankbookCombatPowerPanel:setVisible(true)
	end
end

function HandbookView:reOpen()
	if self._selectedSubKey == directoryChallengeConst then
		executeFunc(self._onOperate,"getChallengeLVInfo")
	end
end

function HandbookView:getCurSelectedSubKey()
	return self._selectedSubKey
end

return HandbookView